package com.caigou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.caigou.bean.cms.entity.FooterConfig;
import com.caigou.bean.cms.param.FooterConfigParam;
import com.caigou.component.HtmlBuilderComponent;
import com.caigou.mapper.cms.FooterConfigMapper;
import com.caigou.service.FooterConfigService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("footerConfigService")
public class FooterConfigServiceImpl implements FooterConfigService {

    @Resource
    private FooterConfigMapper footerConfigMapper;

    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;

    public FooterConfig selectByPrimaryKey(int id){
        return footerConfigMapper.selectByPrimaryKey(id);
    }

    public Result deleteByPrimaryKey(int id){
        int result = footerConfigMapper.deleteByPrimaryKey(id);
        if(result>0){
            return Result.success("删除页脚文字成功");
        }else{
            return Result.error("删除页脚文字失败", "删除页脚文字失败");
        }
    }

    public int insertSelective(FooterConfig footerConfig){
        return footerConfigMapper.insertSelective(footerConfig);
    }

    public Result saveFooterConfig(FooterConfigParam param){
        FooterConfig footerConfig = new FooterConfig();
        BeanUtil.copyProperties(param, footerConfig);
        Date currentTime = new Date(System.currentTimeMillis());
        footerConfig.setCreateTime(currentTime);
        footerConfig.setUpdateTime(currentTime);
        int result = this.insertSelective(footerConfig);
        if(result>0){
            return Result.success("增加页脚设置成功");
        }else{
            return Result.error("增加页脚设置失败", "增加页脚设置失败");
        }
    }

    public Result saveFooterImage(FooterConfigParam param){
        int count = footerConfigMapper.getCountByType(1);
        if(count>0){
            return Result.error("页脚logo图片已经存在，不能再增加了", "页脚logo图片已经存在，不能再增加了");
        }
        FooterConfig footerConfig = new FooterConfig();
        BeanUtil.copyProperties(param, footerConfig);
        footerConfig.setFooterType(1);//1页脚logo图片
        Date currentTime = new Date(System.currentTimeMillis());
        footerConfig.setCreateTime(currentTime);
        footerConfig.setUpdateTime(currentTime);
        int result = this.insertSelective(footerConfig);
        if(result>0){
            return Result.success("增加页脚logo图片成功");
        }else{
            return Result.error("增加页脚logo图片失败", "增加页脚logo图片失败");
        }
    }

    public Result saveFooterText(FooterConfigParam param){
        FooterConfig footerConfig = new FooterConfig();
        BeanUtil.copyProperties(param, footerConfig);
        footerConfig.setFooterType(0);//0页脚文字
        Date currentTime = new Date(System.currentTimeMillis());
        footerConfig.setCreateTime(currentTime);
        footerConfig.setUpdateTime(currentTime);
        int result = this.insertSelective(footerConfig);
        if(result>0){
            return Result.success("增加页脚文字成功");
        }else{
            return Result.error("增加页脚文字失败", "增加页脚文字失败");
        }
    }


    public int updateFooterConfig(FooterConfig footerConfig){
        return footerConfigMapper.updateFooterConfig(footerConfig);
    }

    public Result updateFooterImage(FooterConfigParam param){
        FooterConfig footerConfig = new FooterConfig();
        BeanUtil.copyProperties(param, footerConfig);
        footerConfig.setUpdateTime(new Date(System.currentTimeMillis()));
        int result = this.updateFooterConfig(footerConfig);
        if(result>0){
            return Result.success("更新页脚logo图片成功");
        }else{
            return Result.error("更新页脚logo图片失败", "更新页脚logo图片失败");
        }
    }

    public Result updateFooterText(FooterConfigParam param){
        FooterConfig footerConfig = new FooterConfig();
        BeanUtil.copyProperties(param, footerConfig);
        footerConfig.setUpdateTime(new Date(System.currentTimeMillis()));
        int result = this.updateFooterConfig(footerConfig);
        if(result>0){
            return Result.success("更新页脚文字成功");
        }else{
            return Result.error("更新页脚文字失败", "更新页脚文字失败");
        }
    }

    public Result listFooterText(){
        List<FooterConfig> list = footerConfigMapper.listFooterConfigByType(0);
        return Result.success(list);
    }

    public Result getFooterImage(){
        List<FooterConfig> list = footerConfigMapper.listFooterConfigByType(1);
        return Result.success(list.get(0));
    }

    public Result publish(){
        String url = htmlBuilderComponent.buildFooterHtml();
        log.info("发布页脚内容的url={}", url);
        return Result.success("发布页脚内容成功");
    }
}
